import headerNavTpl from './headerNav.html';

function headerNavComponent($rootScope, $log, $http, ngDialog, authenticate) {
  'ngInject';
  var directive = {
    restrict: 'E',
    replace: true,
    templateUrl: headerNavTpl,
    controller: headerNavController,
    controllerAs: 'headerNav',
    bindToController: true
  };

  function headerNavController () {
    'ngInject';
    
    // 判断一级菜单应该用什么icon
    this.getIconClass = (menuItem) => {
      if (menuItem.name === '首页') {
        return 'fa fa-home';
      } else if (menuItem.name === '人员管理') {
        return 'fa fa-users';
      } else if (menuItem.name === '设备管理') {
        return 'fa fa-cube';
      } else if (menuItem.name === '内业资料管理') {
        return 'fa fa-book';
      } else if (menuItem.name === '安全活动') {
        return 'fa fa-user-md';
      } else if (menuItem.name === '统计分析') {
        return 'fa fa-pie-chart';
      } else if (menuItem.name === '考核设置') {
        return 'fa fa-building-o';
      }
    };

    // 菜单数据
    this.menus = [
      {
        name: '首页',
        srefName: 'main',
        subMenus: [
          // {
          //   name: '二级菜单一',
          // },
          // {
          //   name: '二级菜单一',
          // },
          // {
          //   name: '二级菜单一',
          // },
        ],
      },
      {
        name: '人员管理',
        srefName: 'staffMgt',
        subMenus: [
          {
            name: '人员信息',
            srefName: 'workerInfo',
            parent: '人员管理',
          },
          {
            name: '教育培训',
            srefName: 'techTraining',
            parent: '人员管理',
          },
          {
            name: '安全交底',
            srefName: 'safeDisclosure',
            parent: '人员管理',
          },
          {
            name: '施工队信息',
            srefName: 'constructionTeam',
            parent: '人员管理',
          },
        ],
      },
      {
        name: '设备管理',
        srefName: 'equipMgt',
        subMenus: [
          {
            name: '设备信息',
            srefName: 'equipmentInfo',
            parent: '设备管理',
          },
          {
            name: '维修保养',
            srefName: 'maintenance',
            parent: '设备管理',
          },
          {
            name: '设备检验',
            srefName: 'equipmentInspection',
            parent: '设备管理',
          },
        ],
      },
      {
        name: '安全活动',
        srefName: 'safeActivity',
        subMenus: [
          {
            name: '专项检查',
            srefName: 'specialCheck',
            parent: '安全活动',
          },
          {
            name: '日常巡查',
            srefName: 'dailyCheck',
            parent: '安全活动',
          },
          {
            name: '安全会议',
            srefName: 'meeting',
            parent: '安全活动',
          },
        ],
      },
      {
        name: '内业资料管理',
        srefName: 'documentMgt',
        subMenus: [],
      },
      {
        name: '统计分析',
        srefName: 'statisticAnalyze',
        subMenus: [
          // {
          //   name: '二级菜单一',
          // },
          // {
          //   name: '二级菜单一',
          // },
          // {
          //   name: '二级菜单一',
          // },
        ],
      },
      {
        name: '考核设置',
        srefName: 'checkConfiguration',
        subMenus: [
        ],
      },
    ];


    /** 方法 */

    /** 一级菜单hover时添加hover样式判断 */
    this.setSubMenuActive = (superMenuItem, flag) => {
      this.menus.forEach((item) => {
        if (item.name === superMenuItem.name) {
          item.hover = flag;
        }
      });
    };

    /** 一级菜单visited样式判断 */
    this.isVisited = (superMenuItem) => {
      let currentViewName = $state.$current.name;
      let isVisited = false;
      // 如果是一级菜单页面 直接返回true
      if (superMenuItem.srefName == currentViewName) {
        isVisited = true;
      }

      // 如果是二级菜单页面,遍历二级菜单
      superMenuItem.subMenus.forEach((item) => {
        if (item.srefName == currentViewName) {
          isVisited = true;
        }
      });

      return isVisited;
    };

    /** 是否有二级菜单 */
    this.hasSubMenus = (superMenu) => {
      let subMenus = superMenu.subMenus;
      return subMenus ? (subMenus.length == 0 ? false : true) : false;
    };

    /** 返回当前所在页面即父页面名称 */
    this.getCurrentLocationStacks = () => {
      let locationStacks = {superMenuName: '', subMenuName: ''};
      let currentViewName = $state.$current.name;
      this.menus.forEach((item) => {
        if (item.srefName == currentViewName) {
          locationStacks.superMenuName = item.name;
        }
      });
      if (locationStacks.superMenuName) return locationStacks.superMenuName;
      this.menus.forEach((item) => {
        item.subMenus.forEach((subItem) => {
          if (subItem.srefName == currentViewName) {
            locationStacks.superMenuName = subItem.parent;
            locationStacks.subMenuName = subItem.name;
            return;
          }
        })
      });
      if (!locationStacks.superMenuName || !locationStacks.subMenuName) {
        return '';
      } else {
        return `${locationStacks.superMenuName} > ${locationStacks.subMenuName}`;
      }
    }

    this.changePwd = () => {
      ngDialog.open({
      template: 'pwdDialog',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'vm',
      controller: function trainClarifyCtrl($rootScope, $http, $scope) {
        'ngInject';
        this.userInfo = {};

        // 获取登陆用户信息GET /api/safesite/user/getCurrentUser 获取登录用户

        this.getUserInfo = () => {
          $http
              .get(`${SERVER_CONTEXT}/api/safesite/user/getCurrentUser`)
              .then((resp) => {
                if(resp.statusText == 'OK'){
                  this.userInfo.loginName= resp.data.loginName;
                } 
              });
        }
        // 保存修改密码
        this.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;
          // 表单校验
          if (param.$invalid) {
            return false;
          } else if (param.confirmPassword.$viewValue != param.newPassWord.$viewValue ) {
            layer.alert('确认密码和新密码不一致');
            return false;
          } else {
            $http
                .put(`${SERVER_CONTEXT}/api/safesite/user/updateUser`,
                  this.userInfo)
                .then((resp) => {
                  if (resp.data.success) {
                    layer.msg(resp.data.message);
                    $scope.closeThisDialog();
                    $state.go('login');
                  } else {
                    layer.alert(resp.data.message);
                  }
                });
          }
        };

        // 初始化页面
        this.initPage = () => {
        // 获取登陆用户信息
        this.getUserInfo();
        }

        this.initPage();       

        }
      });
    }

    // 二维码
    this.qrCode = () => {
      ngDialog.openConfirm({
        template: 'codeDialog',
        className: 'ngdialog-theme-default pa-confirm-theme codeDialog',
        controllerAs: 'vm',
        controller: function($scope) {
          'ngInject';

          // 下载用户手册
          this.getDocx = () => {
            return `${SERVER_IP}/uploadfile/平安工程APP用户手册.docx`; 
          }
        },
      })
    }

    // 退出
    this.logout = () => {
      authenticate.logout();
    }
  }
  return directive;
}

export default headerNavComponent;
